<template>
  <div class="horizontal-function-card">
    <!-- 请求部分 - 左侧 -->
    <div class="request-section">
      <h3>Function Call Request</h3>
      <div class="request-details">
        <div class="detail-row">
          <span class="label">Correlation ID:</span>
          <span class="value">{{ input.correlation_id }}</span>
        </div>
        <div class="detail-row">
          <span class="label">Function Name:</span>
          <span class="value">{{ input.data?.name }}</span>
        </div>
        <div class="detail-row">
          <span class="label">Arguments:</span>
          <pre class="value">{{ prettyArgs }}</pre>
        </div>
      </div>
    </div>

    <!-- 响应部分 - 右侧 -->
    <div class="response-section">
      <h3>Function Call Response</h3>
      <div class="response-form">
        <div class="form-row">
          <div class="form-group status-group">
            <label>Status:</label>
            <div class="radio-group">
              <label>
                <input type="radio" v-model="response.status" value="success" />
                Success
              </label>
              <label>
                <input type="radio" v-model="response.status" value="failure" />
                Failure
              </label>
            </div>
          </div>


          <div class="form-group result-group">
            <label>Result (optional):</label>
            <textarea v-model="response.result" placeholder="Enter result message."></textarea>
          </div>

          <div class="button-container">
            <button @click="sendResponse" class="submit-btn">Send Response</button>
          </div>
        </div> 
      </div>
    </div>
    
  </div>
</template>

<script lang="ts">
export default {
  name: 'FunctionTest'
}
</script>

<script lang="ts" setup>
import { computed, reactive } from 'vue'

const props = defineProps({
  input: {
    type: Object,
    required: true,
    default: () => ({
      correlation_id: '',
      data: {
        name: '',
        args: {}
      },
      route: 'response'
    })
  }
})

const emit = defineEmits(['response'])

const response = reactive({
  correlation_id: props.input.correlation_id,
  status: 'success',
  result: '',
  route: props.input.route
})

const prettyArgs = computed(() => {
  return JSON.stringify(props.input.data.args, null, 2)
})

const sendResponse = () => {
  const responseData = {
    correlation_id: response.correlation_id,
    status: response.status,
    result: response.result,
    route: response.route 
  }
  emit('response', responseData)
}
</script>

<style scoped>

.horizontal-function-card {
  display: flex;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  background-color: #f9f9f9;
  min-height: 200px;
}

.request-section, .response-section {
  padding: 16px;
  flex: 1;
}

.request-section {
  border-right: 1px solid #e0e0e0;
  min-width: 250px;
}

h3 {
  margin-top: 0;
  color: #333;
  border-bottom: 1px solid #eee;
  padding-bottom: 8px;
  margin-bottom: 12px;
}

.detail-row {
  display: flex;
  margin-bottom: 8px;
}

.label {
  font-weight: bold;
  min-width: 100px;
  color: #555;
}

.value {
  flex: 1;
}

pre {
  background-color: #f5f5f5;
  padding: 8px;
  border-radius: 4px;
  margin: 0;
  white-space: pre-wrap;
  max-height: 100px;
  overflow-y: auto;
}

.form-row {
  display: flex;
  gap: 16px;
  margin-bottom: 12px;
}

.form-group {
  margin-bottom: 12px;
}

.status-group {
  min-width: 150px;
}

.message-group {
  flex-grow: 1;
}

.result-group {
  margin-bottom: 0;
}

label {
  display: block;
  margin-bottom: 4px;
  font-weight: bold;
  font-size: 14px;
}

input[type="text"] {
  width: 100%;
  padding: 8px;
  border: 1px solid #ddd;
  border-radius: 4px;
}

textarea {
  width: 100%;
  padding: 8px;
  border: 1px solid #ddd;
  border-radius: 4px;
  min-height: 60px;
  resize: vertical;
}

.radio-group {
  display: flex;
  gap: 12px;
}

.radio-group label {
  display: flex;
  align-items: center;
  gap: 4px;
  font-weight: normal;
  cursor: pointer;
}

.button-container {
  display: flex;
  justify-content: flex-end;
  margin-top: 8px;
}

.submit-btn {
  background-color: #4CAF50;
  color: white;
  padding: 8px 16px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  transition: background-color 0.3s;
}

.submit-btn:hover {
  background-color: #45a049;
}
</style>